﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RPGMapa.Data;

namespace RPGMapa.Controls
{
    public partial class TilesetPicker : UserControl
    {
        TabControl tabManager;
        Tile selectedTile;


        public Tile SelectedTile
        {
            get { return selectedTile; }
        }
        public TabControl TabManager
        {
            get { return tabManager; }
            set { tabManager = value; }
        }
        public TilePicker SelectedTilePicker
        {
            get { return (TilePicker)tabManager.SelectedTab.Controls[0]; }
        }
        public ImgMatrix SelectedTileset
        {
            get { return ((TilePicker)tabManager.SelectedTab.Controls[0]).Tileset; }
        }
        public int SelectedIndex
        {
            get { return tabManager.SelectedIndex; }
        }

        public TilesetPicker()
        {
            InitializeComponent();
            tabManager = new TabControl();
            tabManager.Location = new Point(0, 25);
            tabManager.Size = new Size(this.Width, this.Height - 25);
            tabManager.Controls.Clear();

            this.Controls.Add(tabManager);
            this.AddTab(null);
        }

        public void OnTilePicker_MouseClick(Object sender, MouseEventArgs e)
        {
            Vector2 v = NearestNeighbour(e.Location);
            selectedTile = new Tile(SelectedTileset[v], v);
            selectedTile.Passable = checkBox1.Checked;
            SelectedTilePicker.DrawSelectedSquare(e.Location);
        }

        public void AddTab(ImgMatrix tileset)
        {
            TabPage tp = new TabPage();
            TilePicker tpicker = new TilePicker();

            tp.Location = new Point(4, 22);
            tp.Size = new Size(224, 325);
            tp.Text = "Nome Defaut";
            tp.UseVisualStyleBackColor = true;
            tp.AutoScroll = true;

            tpicker.BackColor = Color.Gray;
            tpicker.BorderStyle = BorderStyle.FixedSingle;
            tpicker.Location = new Point(0, 0);
            tpicker.Size = new Size(220, 250); // 220 319
            if (tileset == null)
                tpicker.Tileset = ImgMatrix.DefautTileset();
            else
                tpicker.Tileset = tileset;

            tpicker.Width = tpicker.Tileset.Width;
            tpicker.Height = tpicker.Tileset.Height;
            tpicker.gBackBuffer.DrawImage(tpicker.Tileset.Tileset, Point.Empty);
            tpicker.MouseClick += new MouseEventHandler(OnTilePicker_MouseClick);

            tp.Controls.Add(tpicker);
            tabManager.Controls.Add(tp);
            tabManager.SelectedTab = tp;
        }

        public void RemoveTab()
        {
            tabManager.Controls.Remove(tabManager.SelectedTab);
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            Forms.OpenTileset op = new Forms.OpenTileset();
            op.ShowDialog();
            if (op.Result() != null)
            {
                AddTab(op.Result());
            }
            else
            {
                AddTab(null);
            }
        }

        private void removeButton_Click(object sender, EventArgs e)
        {
            RemoveTab();
        }

        public Vector2 NearestNeighbour(int x, int y)
        {   // we now that each tab has only one control (index 0) and that it is a TilePicker, so 100% safe cast.
            Size tileSize = SelectedTilePicker.TileSize;
            return new Vector2((x - x % tileSize.Width) / tileSize.Width, (y - y % tileSize.Height) / tileSize.Height);
        }
        public Vector2 NearestNeighbour(Point p)
        {
            return NearestNeighbour(p.X, p.Y);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            selectedTile.Passable = checkBox1.Checked;
        }

        private void TilesetPicker_MouseMove(object sender, MouseEventArgs e)
        {
            SelectedTilePicker.Focus();
        }
    }
}
